package com.code.ddbond.service;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.code.ddbond.mapper.UserInfoMapper;
import com.code.ddbond.vo.RegisterVo;
import com.code.ddbond.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Objects;

@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    private String salt = "snuist";

    @Override
    public boolean register(RegisterVo registerVo)
    {
        UserInfo existingUser = userInfoMapper.selectOne
                (
                        new QueryWrapper<UserInfo>().eq("login_name", registerVo.getLoginname())
                );
        if (existingUser != null)
        {
            return false;
        }

        UserInfo newUser = new UserInfo();
        newUser.setLoginname(registerVo.getLoginname());
        String password = registerVo.getPassword() + salt;//加盐
        String s = SecureUtil.md5(password);             //将放在数据库的密码放之前加密
        newUser.setPwd(s);
        newUser.setEmail(registerVo.getEmail());
        // 插入数据库
        return save(newUser);
    }

    public boolean checkLoginnameExists(String loginname) {
        return userInfoMapper.existsByLoginname(loginname);
    }

    @Override
    public int login(String loginName, String password)
    {
        UserInfo user = userInfoMapper.selectOne(new QueryWrapper<UserInfo>().eq("login_name", loginName));
        String s=SecureUtil.md5(password+salt);
        if (user != null && user.getPwd().equals(s))
        {
            return 1;
        }
        else
            return -1;
    }
    @Override
    public UserInfo getUserByLoginName(String loginName)
    {
        return userInfoMapper.selectOne(new QueryWrapper<UserInfo>().eq("login_name", loginName));
    }

    @Override
    @Transactional
    public void updateUserById(UserInfo userInfo)
    {
        UserInfo existingUser = this.getById(userInfo.getId());
        if (existingUser == null)
        {
            throw new IllegalArgumentException("用户ID为 " + userInfo.getId() + " 的用户不存在");
        }

        if (Objects.nonNull(userInfo.getLoginname())) {
            existingUser.setLoginname(userInfo.getLoginname());}
        if (Objects.nonNull(userInfo.getUsername())) {
            existingUser.setUsername(userInfo.getUsername());}
        if (Objects.nonNull(userInfo.getPwd())) {
            String s=SecureUtil.md5(userInfo.getPwd());
            existingUser.setPwd(s);}
        if (Objects.nonNull(userInfo.getEmail())) {
            existingUser.setEmail(userInfo.getEmail());}
        if (Objects.nonNull(userInfo.getGender())) {
            existingUser.setGender(userInfo.getGender());}
        this.updateById(existingUser);
    }

    @Override
    public UserInfo getUserInfoByName(String username)
    {
        UserInfo userInfo = userInfoMapper.selectByUserName(username);
        return userInfo;
    }

    @Override
    public boolean checkEmailForPasswordRecovery(String email) {//邮箱是否存在
        return userInfoMapper.checkEmailExistence(email) > 0;
    }

    @Override
    public void resetPassword(String email, String newPassword)
    {
        String sk = SecureUtil.md5(newPassword + salt);
        int rowsAffected = userInfoMapper.updatePasswordByEmail(email, sk);
        if (rowsAffected == 0)
        {
            throw new IllegalArgumentException("更新此邮箱密码失败: " + email);
        }
    }

}
